package com.eureka.eurekaserviceinvoker.service;

import com.eureka.eurekaserviceinvoker.entity.Person;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 合并请求
 * Created by earl on 2018/5/2.
 */
@Component
public class CollapserService {

    @HystrixCollapser(
            batchMethod = "getPerson",
            collapserProperties = {
                    @HystrixProperty(name = "timerDelayInMilliseconds", value = "1000")
            }
    )
    public Future<Person> getSinglePerson(Long id) {
        System.out.println("-----执行单个获取的方法-----");
        Person p = new Person();
        p.setId(id);
        p.setName(" i am "+id);
        return  null;
    }

    @HystrixCommand
    public List<Person> getPerson(List<Long> ids) {
        System.out.println("-----收集请求 参数数量" + ids.size() + " -----");
        //模拟HTTP请求
        List<Person> list = new ArrayList<>(10);
        ids.forEach(id->{
            Person p = new Person();
            p.setId(id);
            p.setName(" i am "+id);
            list.add(p);
        });
        return list;
    }


}

